package org.xliu.cs.projects.anno_for_doc.maven_plugins;

import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xliu.cs.projects.anno_for_doc.generate.AbstractGenerator;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;

public class MavenGenerator extends AbstractGenerator {
    private static final Logger log = LoggerFactory.getLogger(AnnotationMojo.class);

    private final URL[] urls;

    public MavenGenerator(MavenProject project) throws DependencyResolutionRequiredException, MalformedURLException {
        // if the requiresDependencyResolution is COMPILE, then here will include the maven dependency jars.
        List<String> classpathElements = project.getCompileClasspathElements();

        urls = new URL[classpathElements.size()];

        int index = 0;
        for (String classpathElement : classpathElements) {
            log.debug("classloader add url {}", classpathElement);
            urls[index++] = new File(classpathElement).toURI().toURL();
        }
    }

    @Override
    protected ClassLoader getClassLoader() {
        return new URLClassLoader(urls, getClass().getClassLoader());
    }
}
